<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Initialize Messages that will be shown on the team screen.
 * @access public 
 */
function init_team()
{
    $ci = &get_instance();
    $cms_config = $ci->cms;

    // company_id
    $ci->company_id = $ci->session->userdata('company_id') ? $ci->session->userdata('company_id') : NULL;
    
    // initialize team messages
    init_messages('Team');
    
    // initialize table query table columns
    $ci->alias  = '';
    $ci->alias .= $cms_config['db_prefix'] . 'teams.id AS id,';
    $ci->alias .= $cms_config['db_prefix'] . 'teams.name AS name,';
    $ci->alias .= $cms_config['db_prefix'] . 'teams.description AS description,';
    $ci->alias .= $cms_config['db_prefix'] . 'teams.date_created AS date_created,';
    $ci->alias .= $cms_config['db_prefix'] . 'teams.date_modified AS date_modified,';
    $ci->alias .= $cms_config['db_prefix'] . 'teams.status AS status ';
    
    // initialize search columns
//    $ci->search_columns = array('name', 'description', 'status');
    
    // option buttons
    $ci->options_markup  = '';
    $ci->options_markup .= '<a href="' . base_url() . 'teams/preview/id/$1" class="btn btn-info btn-xs with-modal btn-preview" rel="tooltip" title="Preview Record"><i class="fa fa-search"></i></a>' . "\n";
    $ci->options_markup .= '<a href="' . base_url() . 'teams/edit/id/$1" class="btn btn-primary btn-xs with-modal btn-edit" rel="tooltip" title="Edit Record"><i class="fa fa-pencil"></i></a>' . "\n";
    $ci->options_markup .= '<a href="' . base_url() . 'teams/delete/id/$1" class="btn btn-danger btn-xs with-modal btn-delete"rel="tooltip" title="Delete Record"><i class="fa fa-trash-o"></i></a>';

    // initialize table headers
//    $table_headers                      = array();
//    $table_headers['name']              = 'Name';
//    $table_headers['description']       = 'Description';
//    $table_headers['date_created']      = 'Date Created';
//    $table_headers['date_modified']     = 'Date Modified';
//    $table_headers['status']            = 'Status';
//    $ci->table_headers                  = create_datatable_headers($table_headers, 'teams', 2);

    // initialize customized table columns
    $ci->custom_columns = array();
    
    $date_created                           = array();
    $date_created['keys']                   = 'cms_date(date_created)';
    $date_created['markup']                 = '$1';
    $ci->custom_columns['date_created']     = $date_created;
    
    $date_modified                          = array();
    $date_modified['keys']                  = 'cms_date(date_modified)';
    $date_modified['markup']                = '$1';
    $ci->custom_columns['date_modified']    = $date_modified;
    
//    $status['keys'] = 'cms_status_toggle(status, teams/confirm_update/, id)';
//    $status['markup'] = '$1';
//    $ci->custom_columns['status'] = $status;
}

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_add_team()
{
    $ci = &get_instance();
    $cms = $ci->cms;

    $ci->form_validation
            ->set_rules('company',      'company',      'trim|required|numeric|max_length[10]|xss_clean')
            ->set_rules('name',         'name',         'trim|required|max_length[20]|xss_clean')
            ->set_rules('description',  'description',  'trim|required|max_length[1000]|xss_clean')
            ->set_rules('status',       'status',       'trim|max_length[10]|xss_clean');
            
    return $ci->form_validation->run();
}

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_edit_team()
{
    $ci = &get_instance();
    
    $ci->input->post('id')           ? $ci->form_validation->set_rules('id',             'ID',           'trim|required|numeric|max_length[10]|xss_clean')   : '';
    $ci->input->post('company')      ? $ci->form_validation->set_rules('company',        'company',      'trim|required|numeric|max_length[20]|xss_clean')   : '';
    $ci->input->post('name')         ? $ci->form_validation->set_rules('name',           'name',         'trim|required|max_length[20]|xss_clean')           : '';
    $ci->input->post('description')  ? $ci->form_validation->set_rules('description',    'description',  'trim|required|max_length[1000]|xss_clean')         : '';
    $ci->input->post('status')       ? $ci->form_validation->set_rules('status',         'status',       'trim|max_length[10]|xss_clean')                    : '';
    
    return $ci->form_validation->run();
}

/**
 * Displays teams list page.
 * @access public
 * @param array $content An array of teams to be shown.
 * @param string $message The type of message to be shown.
 */
function view_teams_list($content = NULL, $message_type = NULL)
{
    $ci = &get_instance();
    
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message']            = $message;
    
    if($content === NULL)
    {
        $args = array();
        $args['status'] = 'active';
        $ci->company_id !== NULL ? $args['company_id'] = $ci->company_id : '';
     
        $data['teams'] = $ci->team_model->all($args)->get();
    } else
    {
        $data['teams'] = $content;
    }

    // active navigation
    $data['navigation'] = 'teams';

    // create breadcrumbs
    clear_breadcrumbs();
    store_breadcrumbs('Teams', 'teams');
    build_breadcrumbs();
    
    // build template
    $ci->template
            ->title('Teams')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('teams', 'team_table')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
            ->set_partial('modal', 'layouts/modal')
            ->build('team_list', $data);
}

/**
 * Displays team info.
 * @access public
 * @param int $team_id ID of the record to be shown.
 */
function view_team_preview($team_id)
{
    $ci = &get_instance();
    
    // team info
    $data = array();
    $data['team_infos'] = $ci->team_model->find($team_id)->get();

    // active navigation
    $data['navigation'] = 'teams';

    // create breadcrumbs
    store_breadcrumbs('Preview Team', 'teams/preview/id/' . $team_id);
    build_breadcrumbs();
    
    // build template
    $ci->template
            ->title('Team')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
//            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('team_preview', $data);
}

/**
 * Displays the form for adding or editing teams.
 * @access public
 * @param string $title String representing the title page.
 * @param string $crumb The string that will be appended to the end of the breadcrumb.
 * @param string $message The type of message that will be shown on the form. Default value is null.
 * @param array $record An array containing record info to be edited. Default value is null.
 */
function view_team_form($title, $message_type = NULL, $record = NULL)
{
    $ci = &get_instance();
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message']            = $message;

    // set form data
    $data['action']             = $title == 'Add Team' ? 'add' : 'edit';
    $data['add']                = $title == 'Add Team' ? TRUE : FALSE;
    $data['add_another']        = $ci->input->post('add-another') ? ' checked="checked"' : '';

    // set company information
    if ( $ci->ion_auth->is_admin() )
    {
        $data['company_options']    = $ci->company_model->all()->get();
        $data['company']            = isset($record['company_id']) ? $record['company_id'] : set_value('company_id');
        $data['company-status']     = form_error('company') ? ' has-error' : '';
    }

    // set team data
    $data['id']                 = isset($record['id']) ? $record['id'] : set_value('id');
    
    $data['name']               = isset($record['name']) ? $record['name'] : set_value('name');
    $data['name-status']        = form_error('name') ? ' has-error' : '';
    
    $data['description']        = isset($record['description']) ? $record['description'] : set_value('description');
    $data['description-status'] = form_error('description') ? ' has-error' : '';
    
    $data['status']             = isset($record['status']) ? $record['status'] : set_value('status');
    
    // set active navigation
    $data['navigation'] = 'teams';
    
    // create breadcrumbs
    store_breadcrumbs('Add Teams', 'teams/add');
    build_breadcrumbs();

    // build template
    $ci->template
            ->title($title)
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('team_form', $data);
    
}

/**
 * Formats an array of records to valid response.
 * @access public
 * @param array $teams Array of teams
 */
function response_teams($teams)
{
    $ci = &get_instance();
    
    $params = $ci->input->post();
    $case = isset( $params['case'] ) ? $params['case'] : 'snake';
    
    $response_teams = array();
    foreach($teams as $team)
    {
        $response_team                   = array();
        isset($team['id'])               ? $response_team[changeCase('id', $case)]               = clean( $team['id'] ) : '';
        isset($team['name'])             ? $response_team[changeCase('name', $case)]             = clean( $team['name'] ) : '';
        isset($team['description'])      ? $response_team[changeCase('description', $case)]      = clean( $team['description'] ) : '';
        isset($team['date_created'])     ? $response_team[changeCase('date_created', $case)]     = clean( $team['date_created'] ) : '';
        isset($team['date_modified'])    ? $response_team[changeCase('date_modified', $case)]    = clean( $team['date_modified'] ) : '';
        isset($team['status'])           ? $response_team[changeCase('status', $case)]           = clean( $team['status'] ) : '';

        array_push($response_teams, $response_team);
    }
    
    // we format our response
    if( count($response_teams) > 0 ) // check if not empty
    {
        format_response('success', 200, 'Response Teams.', NULL, $response_teams);
    }
    else
    {
        format_response('success', 200, 'No Data Available', NULL, 'No Data Available');
    }
    
}


/* End of file team_helper.php */
/* Location: ./application/teams/backend-teams/helpers/team_helper.php */